<template>
  <el-dialog
    :title="!dataForm.choosePropertyValueId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px">
      <el-form-item label="属性值" prop="choosePropertyValueName" label-width="150px">
        <el-input v-model="dataForm.choosePropertyValueName" placeholder="属性值"></el-input>
      </el-form-item>
     <el-form-item label="状态" size="mini" prop="choosePropertyValueState" label-width="150px">
        <el-radio-group v-model="dataForm.choosePropertyValueState">
          <el-radio :label="0">启用</el-radio>
          <el-radio :label="1">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" :disabled="submitVisible" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        submitVisible: false,
        dataForm: {
          choosePropertyValueId: '',
          choosePropertyValueName: '',
          choosePropertyValueState: '',
          choosePropertyId: ''
        },
        dataRule: {
          choosePropertyValueName: [
            { required: true, message: '属性值不能为空', trigger: 'blur' }
          ],
          choosePropertyValueState: [
            { required: true, message: '属性值状态', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (choosePropertyId, id) {
        this.dataForm.choosePropertyId = choosePropertyId
        this.dataForm.choosePropertyValueState = 0
        this.dataForm.choosePropertyValueId = id || ''
        this.visible = true
        this.submitVisible = false
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
        console.log(choosePropertyId)
        if (this.dataForm.choosePropertyValueId) {
          this.$http({
            url: this.$http.adornUrl(`/pig-center-biz/shopset/goodsChoosePropertyValue/info`),
            method: 'post',
            params: this.$http.adornParams({
              'choosePropertyValueId': this.dataForm.choosePropertyValueId
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.choosePropertyValueName = data.goodsChoosePropertyValue.choosePropertyValueName
              this.dataForm.choosePropertyValueState = data.goodsChoosePropertyValue.choosePropertyValueState
            }
          })
        }
      },

      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.submitVisible = true
            this.$http({
              url: this.$http.adornUrl(`/pig-center-biz/shopset/goodsChoosePropertyValue/${!this.dataForm.choosePropertyValueId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'choosePropertyValueId': this.dataForm.choosePropertyValueId || undefined,
                'choosePropertyValueName': this.dataForm.choosePropertyValueName,
                'choosePropertyValueState': this.dataForm.choosePropertyValueState,
                'choosePropertyId': this.dataForm.choosePropertyId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
<style>

</style>
